package com.bc.bcapi.service;

import com.bc.bcapi.dao.T_Project_analysis_relationMapper;
import com.bc.bcapi.dao.T_User_elitesMapper;
import com.bc.bcmodel.domain.T_Project_analysis_relationModel;
import com.bc.bcmodel.req.ElitesFavoriteCountReq;
import com.bc.bcmodel.req.ElitesFavoriteReq;
import com.bc.commonentity.common.JsonResult;
import com.bc.commonentity.enums.HttpReturnRnums;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.math.BigInteger;

import com.bc.bcapi.dao.T_User_elites_relationMapper;
import com.bc.bcmodel.domain.T_User_elites_relationModel;

/**
 * @Name T_User_elites_relationService * @Description  * @Author generation * @Date 2018/8/30 15:34:20 * @Version 1.0
 **/
@Service
public class T_User_elites_relationService {
    @Resource
    T_User_elites_relationMapper t_User_elites_relationMapper;
    @Resource
    T_User_elitesMapper t_user_elitesMapper;

    /**
     * 保存单条数据     *      * @return
     */
    public int insert(T_User_elites_relationModel req) {
        return t_User_elites_relationMapper.insert(req);
    }

    /**
     * 批量增加数据     *      * @return
     */
    public int insertBatch(List<T_User_elites_relationModel> req) {
        return t_User_elites_relationMapper.insertBatch(req);
    }

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    public T_User_elites_relationModel findByNamedParam(T_User_elites_relationModel req) {
        return t_User_elites_relationMapper.findByNamedParam(req);
    }

    /**
     * 根据对象查询所有数据     *      * @return
     */
    public List<T_User_elites_relationModel> getAllList(T_User_elites_relationModel req) {
        return t_User_elites_relationMapper.findByNamedParamList(req);
    }

    /**
     * 根据对象查询所有分页数据     *      * @return
     */
    public PageInfo<T_User_elites_relationModel> getList(T_User_elites_relationModel req) {
        PageHelper.startPage(req.getPage_index(), req.getPage_size());
        List<T_User_elites_relationModel> list = t_User_elites_relationMapper.findByNamedParamList(req);
        return new PageInfo<T_User_elites_relationModel>(list);
    }

    /**
     * 更新指定对象     *      * @return
     */
    public int update(T_User_elites_relationModel req) {
        return t_User_elites_relationMapper.update(req);
    }

    /**
     * 删除指定数据     *      * @return
     */
    public int delete(BigInteger id) {
        return t_User_elites_relationMapper.delete(id);
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    public long count(T_User_elites_relationModel req) {
        return t_User_elites_relationMapper.count(req);
    }

    /**
     * 设置支持人才+收藏人才
     */
    public JsonResult setElitesLike(ElitesFavoriteReq req) {
        JsonResult js = new JsonResult();
        if (req.getElites_user_id().isEmpty() ||
                req.getType_id() <= 0 ||
                req.getType_id() > 2
                ) {
            return new JsonResult(HttpReturnRnums.ParaError);
        }
        if (req.getType_id() == 1) {
//        顶踩
            T_User_elites_relationModel t_user_elites_relationModel = new T_User_elites_relationModel();
            t_user_elites_relationModel.setMy_user_id(req.getMy_user_id());
            t_user_elites_relationModel.setElites_user_id(req.getElites_user_id());
            t_user_elites_relationModel.setType_id(req.getType_id());
            int count = t_User_elites_relationMapper.count(t_user_elites_relationModel);
            if (count > 0) {
                return new JsonResult(HttpReturnRnums.ELITES_EXISTS_FOLLOW);
            }
        }
        int result = t_User_elites_relationMapper.deleteRelation(req);
        ElitesFavoriteCountReq elitesFavoriteCountReq = new ElitesFavoriteCountReq();
        elitesFavoriteCountReq.setType_id(req.getType_id());
        elitesFavoriteCountReq.setUser_id(req.getElites_user_id());
        if (result == 0) {
            //添加
            T_User_elites_relationModel t_user_elites_relationModel
                    = new T_User_elites_relationModel();
            t_user_elites_relationModel.setMy_user_id(req.getMy_user_id());
            t_user_elites_relationModel.setElites_user_id(req.getElites_user_id());
            t_user_elites_relationModel.setType_id(req.getType_id());
            t_User_elites_relationMapper.insert(t_user_elites_relationModel);
            //人才关注数+1
            elitesFavoriteCountReq.setIs_add(1);
        } else {
            //人才关注数-1
            elitesFavoriteCountReq.setIs_add(0);
        }
        t_user_elitesMapper.updateElitesFavoriteCount(elitesFavoriteCountReq);
        return js;
    }
}